#!/usr/bin/env python
# encoding: utf-8

import serial
import threading

ser = serial.Serial("COM1",115200,timeout=0.1)

def recv(func):
    # while True:
    def decorator(recv):
        recv = None
        try:
            recv = ser.read(255)
        except Exception as e:
            print(str(e))
        return func(recv)
    return decorator

def debug(func):
    def wrapper():
        print("[DEBUG]: enter {}()".format(func.__name__))
        return func()
    return wrapper

class de:
    def __init__(self, level):
        self.level = level

    def __call__(self, func):
        def decorator(*args):
            print(self.level)
            print('call me')
            func(*args)
        return decorator

def debug_arg(level='',name=''):
    def decorator(func):
        def decorator_inner(*args,**kwargs):
            if level:
                print(level)
            if name:
                print(name)
            if "test" in kwargs:
                print(args[0],kwargs["test"])
            else:
                print("test key not in")
            return func(*args, **kwargs)
        return decorator_inner
    return decorator
# @debug
# @debug_arg('1','name')
@de('debug')
def say_hello(some):
    print("hello!")
say_hello("test")

def deal_recv(recv):
    print(recv)
data = ""
def test():
    global data
    while True:
        @recv
        def deal(r):
            if r:
                data = str(r)
                deal_recv(data)
            else:
                print("无")
        r = ""
        deal(r)
        # print(data)

threading.Thread(target=test).start()

# @recv
# def deal(r):
    # if r:
        # print(r)
    # else:
        # print("无")
# r = ""
# deal(r)
